﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace Rose.Movies
{
    public class HtmlTag
    {


        public string GetUniqTag(string source, string tag, string character)
        {
            string begin = "<\\s*" + tag;
            string end = "<\\s*/\\s*" + tag + "\\s*>";
            string other = begin;

            int tempcount = 0;

            Regex regex_begin = new Regex(begin + "\\s*" + character, RegexOptions.IgnoreCase);
            Regex regex_other = new Regex(other, RegexOptions.IgnoreCase);
            Regex regex_end = new Regex(end, RegexOptions.IgnoreCase);


            /////begin match
            MatchCollection match_begin = regex_begin.Matches(source);
            //MatchCollection tests = regex_other.Matches(source);
            //Console.WriteLine(tests.Count);
            if (match_begin.Count > 0)
            {
                if (match_begin.Count == 1)
                {
                    tempcount = match_begin[0].Index;
                    source = source.Substring(tempcount);
                    MatchCollection matches_begin = regex_other.Matches(source);
                    MatchCollection matches_end = regex_end.Matches(source);
                    int count_begin = matches_begin.Count;
                    int count_end = matches_end.Count;

                    if (count_end > 0 && count_begin > 0)
                    {
                        int[] int_begin = new int[matches_begin.Count];
                        int[] int_end = new int[matches_end.Count];

                        for (int i = 0; i < count_begin; i++)
                        {
                            int_begin[i] = matches_begin[i].Index; //记录出现的位置
                        }
                        for (int i = 0; i < count_end; i++)
                        {
                            int_end[i] = matches_end[i].Index;//记录出现的位置
                        }
                        bool isend = false;

                        for (int i = 0; i < count_begin - 1; i++)
                        {
                            if (int_begin[i + 1] > int_end[i])
                            {
                                tempcount = int_end[i];
                                isend = true;
                                break;
                            }
                        }

                        if (isend == false)
                        {
                            tempcount = int_end[count_begin - 1];
                        }

                        if (source.Length == tempcount + tag.Length + 3)///because of </> sum 3;
                        {
                            return source;
                        }

                        else
                        {

                            string laststring = source.Substring(tempcount);

                            Regex tempregex = new Regex(">", RegexOptions.IgnoreCase);
                            Match tempmatch = tempregex.Match(laststring);

                            return source.Substring(0, tempcount + tempmatch.Index + 1);
                        }

                    }
                    else return ("no end-tag");


                }
                else return ("want you find is not only one,please check youself");
            }

            else
            {

                return ("no found");
            }


        }

        public string GetIndexTag(string source, string tag, int tagindex = 0)
        {
            string begin_string = "<\\s*" + tag + "[^>]*" + ">";
            string end_string = "</\\s*" + tag + "\\s*>";

            Regex begin_regex = new Regex(begin_string, RegexOptions.IgnoreCase);
            Regex end_regex = new Regex(end_string, RegexOptions.IgnoreCase);


            int begin_count = 0;
            int end_count = 0;
            int tozero = -1;
            int begin_record = 0;
            int end_record = 0;
            bool is_begin_on = true;
            bool is_end_on = true;
            bool is_tozero_on = true;



            MatchCollection temp_matches = begin_regex.Matches(source);


            if (temp_matches.Count <= tagindex)
                return "error-tagindex is to large";

            string tempsource = source.Substring(temp_matches[tagindex].Index);

            MatchCollection begin_matches = begin_regex.Matches(tempsource);
            MatchCollection end_matches = end_regex.Matches(tempsource);

            begin_count = begin_matches.Count;
            int i = 0;
            int j = 0;



            while (tozero != 0)
            {

                if (is_tozero_on)
                {
                    tozero = 0;
                    is_tozero_on = false;
                }
                try
                {

                    int begin_index = begin_matches[i].Index;
                    int end_index = end_matches[j].Index;



                    if (begin_index < end_index)
                    {
                       
                        tozero += 1;
                        i = i + 1;
                        if (is_begin_on)
                        {
                            begin_record = begin_index;
                            is_begin_on = false;
                        }
                    }
                    else
                    {
                     
                        tozero -= 1;
                        j = j + 1;
                        if (tozero == 0)
                        {
                            end_record = end_index;

                        }
                    }
                    if (i == begin_count - 1)
                    {
                        end_record = end_matches[i].Index;
                        break;

                    }//here have some special deal
                }
                catch (Exception ex)
                {
                    throw;
                }

            }
            int length = end_record - begin_record + tag.Length + 3;
            return tempsource.Substring(begin_record, length);



        }

        public List<string> GetTagCollect(string source, string tag, string character)
        {
            List<string> returnModel = new List<string>();

            //test


            string end_string = "</\\s*" + tag + "\\s*>";

            Regex begin_regex = new Regex("<" + tag + "\\s*" + character, RegexOptions.IgnoreCase);

            Regex end_regex = new Regex(end_string, RegexOptions.IgnoreCase);
            string tempstring = source;

            while (1 == 1)
            {
                MatchCollection begin_matches = begin_regex.Matches(tempstring);
                if (begin_matches.Count <= 0)
                    return returnModel;

                int begin = begin_matches[0].Index;
                tempstring = tempstring.Substring(begin);

                string addModel = GetIndexTag(tempstring, tag, 0);

                //FileStream afile = new FileStream(@"C:\Users\Administrator\Desktop\" + (tempcount++) + ".txt", FileMode.Append);
                //StreamWriter writer = new StreamWriter(afile);
                //writer.WriteLine(addModel);
                //writer.WriteLine("-----------------------------------------------");
                //writer.Close();

                returnModel.Add(addModel);

                tempstring = tempstring.Substring(addModel.Length);

            }




        }


        public int GetMaxDeep(string source, string tag = "div")
        {
            string begin_string = "<\\s*" + tag + "[^>]*" + ">";
            string end_string = "</\\s*" + tag + "\\s*>";

            Regex begin_regex = new Regex(begin_string, RegexOptions.IgnoreCase);
            Regex end_regex = new Regex(end_string, RegexOptions.IgnoreCase);

            MatchCollection begin_matches = begin_regex.Matches(source);
            MatchCollection end_matches = end_regex.Matches(source);

            int deep = 0;
            const int END = 0;

            for (int i = 0; i < begin_matches.Count; i++)
            {
                if (begin_matches[i].Index < end_matches[END].Index)
                    deep += 1;
                else break;
            }

            // return begin_matches[deep].Index;

            return deep - 1;
        }

        public List<string> GetImageFromHtml(string source)
        {
            string img = "<img.*src=\"[^>]*>";
            Regex begin_regex = new Regex(img, RegexOptions.IgnoreCase);

            MatchCollection matches = begin_regex.Matches(source);

            List<string> returnModel = new List<string>();

            int imagecount = 0;
            if ((imagecount = matches.Count) <= 0)
                return returnModel;

            for (int i = 0; i < imagecount; i++)
            {
                returnModel.Add(matches[i].Value);
            }

            return returnModel;


        }

       

        public List<string> GetImageSrc(string source)
        {
            List<string> imagelist = GetImageFromHtml(source);
            List<string> returnModel = new List<string>();

            Regex regex = new Regex("http://[^\"]*" + "\"", RegexOptions.IgnoreCase);
            foreach (var item in imagelist)
            {
                MatchCollection matches = regex.Matches(item);
                if (matches.Count > 0)
                    returnModel.Add(matches[0].Value.Replace("\"", ""));

            }
            return returnModel;


        }

        public string GetInfoFromSampleTag(string source, string begin = "http://", string end = "\"")
        {
            Regex regex_begin = new Regex(begin + "[^\"]*" + end, RegexOptions.IgnoreCase);

            MatchCollection matches = regex_begin.Matches(source);

            if (matches.Count <= 0)
                return string.Empty;

            return matches[0].Value.Replace("\"", "");

        }

    }
}